package jcommon.chap4_functor;

import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.NotPredicate;
import org.apache.commons.collections.functors.SwitchTransformer;

public class Transform {

	public static void main(String[] args) {
		System.out.println("-------- switchTransformerDemo --------");
		switchTransformerDemo();
	}

	private static void switchTransformerDemo() {
		Predicate isOdd = new Predicate() {
			public boolean evaluate(Object input) {
				Integer integer = (Integer) input;
				return integer % 2 != 0;
			}
		};

		Predicate isEven = new NotPredicate(isOdd);
		Predicate[] predicates = new Predicate[] { isOdd, isEven };

		Transformer transOdd = new Transformer() {
			public Object transform(Object input) {
				Integer integer = (Integer) input;
				return integer * 3;
			}
		};

		Transformer transEven = new Transformer() {
			public Object transform(Object input) {
				Integer integer = (Integer) input;
				return integer * 2;
			}
		};
		Transformer[] transformers = new Transformer[] { transOdd, transEven };

		Transformer switchTransformer = new SwitchTransformer(predicates,
				transformers, new Transformer() {
					public Object transform(Object arg0) {
						return null;
					}
				});

		for (int i = 0; i < 10; i++) {
			System.out.println(switchTransformer.transform(i));
		}
	}
}
